package Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

/*
* 1. 用ArrayList存储多个套接字连接
* 2. 新建一个类，存放用户名， 游戏状态， 多人排行榜的积分等
* 3. 每次验证登录时新添一个用户类保存
* 4. 用HashMap存储用户类和套接字， 登录后为套接字对应的用户类初始化
* 5. 单独写一个函数，可以返回HashMap中的键值对
* */

/*
*  1. HashMap users 存放用户类和套接字
*  2. HashMap info 存放账户信息
* */

public class Server {
    public static HashMap<String, String> info = new HashMap<>();
    public static HashMap<Socket, User> users = new HashMap<>();
    public static final int MAXPLAYER = 2;
    static ServerSocket server;
    static Socket temp;
    public static ArrayList<SocketThread> threadList = new ArrayList<SocketThread>();

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        loadingInfo();

        server = new ServerSocket(5001);
        while(users.size() < MAXPLAYER){
            temp = server.accept();
            threadList.add(new SocketThread(temp));
            threadList.get(threadList.size() - 1).start();
        }
    }

    public static void loadingInfo() throws IOException {
        File infoFile = new File("src/res/info.txt");
        BufferedReader br = new BufferedReader(new FileReader(infoFile));
        String line;
        while((line = br.readLine()) != null){
            info.put(line.split("=")[0], line.split("=")[1]);
        }
    }
}
